home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume11 / hd_pjr / part01 next >
Encoding:
Text File  |  1990-03-25  |  22.9 KB  |  850 lines

  1. Newsgroups: comp.sources.misc
  2. organization: Pyramid Technology Ltd, Farnborough, ENGLAND
  3. subject: v11i064: hd - hexadecimal dump
  4. From: pjr@pyra.co.uk (Peter Ruczynski)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 11, Issue 64
  8. Submitted-by: pjr@pyra.co.uk (Peter Ruczynski)
  9. Archive-name: hd_pjr/part01
  10.  
  11. [Warning to Xenix users:  rename this before installing.  ++bsa]
  12.  
  13. This program came about because I was not satisfied with the output from
  14. the standard UNIX od utility. It is basically a case of personal preference
  15. but I hope that some people will find this useful and perhaps preferable
  16. to od.
  17.  
  18. I've posted to comp.sources.unix as well but I haven't heard anything for
  19. quite a while from there so I thougth I'd try misc. 
  20.  
  21. enjoy
  22.  
  23. Pete.
  24.  
  25. #! /bin/sh
  26. # This is a shell archive.  Remove anything before this line, then unpack
  27. # it by saving it into a file and typing "sh file".  To overwrite existing
  28. # files, type "sh file -c".  You can also feed this as standard input via
  29. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  30. # will see the following message at the end:
  31. #        "End of shell archive."
  32. # Contents:  Makefile README hd.1 hd.c hexprint.c hexprint.h
  33. # Wrapped by pjr@pyrrot on Thu Mar 15 17:51:27 1990
  34. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  35. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  36.   echo shar: Will not clobber existing file \"'Makefile'\"
  37. else
  38. echo shar: Extracting \"'Makefile'\" \(389 characters\)
  39. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  40. XHOME=/b2/pjr
  41. XCC=cc
  42. XSRC=.
  43. XCFLAGS=-c -DH_OFFSETS -DH_COMPRESS
  44. XCFILES=hd.c hexprint.c
  45. XOBJ=hd.o hexprint.o
  46. X
  47. Xhd:    $(OBJ)
  48. X    $(CC) $(OBJ) -o hd
  49. X
  50. Xinst:    hd
  51. X    cp hd $(HOME)/bin
  52. X
  53. Xhd.o:        hd.c /usr/include/stdio.h /usr/include/sys/types.h\
  54. X        /usr/include/sys/file.h /usr/include/sys/fcntl.h\
  55. X        /usr/include/fcntl.h /usr/include/string.h hexprint.h
  56. X
  57. Xhexprint.o:    hexprint.c /usr/include/stdio.h hexprint.h
  58. X
  59. END_OF_FILE
  60. if test 389 -ne `wc -c <'Makefile'`; then
  61.     echo shar: \"'Makefile'\" unpacked with wrong size!
  62. fi
  63. # end of 'Makefile'
  64. fi
  65. if test -f 'README' -a "${1}" != "-c" ; then 
  66.   echo shar: Will not clobber existing file \"'README'\"
  67. else
  68. echo shar: Extracting \"'README'\" \(2255 characters\)
  69. sed "s/^X//" >'README' <<'END_OF_FILE'
  70. X            ===========================
  71. X            HD Hexadecimal Dump Program
  72. X            ===========================
  73. X
  74. XThis program came about because I was not satisfied with the output from
  75. Xthe standard UNIX od utility. It is basically a case of personal preference
  76. Xbut I hope that some people will find this useful and perhaps preferable
  77. Xto od. It is not quite as flexible as od in that it can only output in hex
  78. Xand ascii but it is more flexible than od in format and control of output.
  79. X
  80. XOne of the the main considerations when writing the hexprint routine was speed,
  81. Xhence the offset printing and compression can be compiled out if the
  82. Xappropriate flags are not defined, giving a faster but simpler dump routine.
  83. XUnfortunately as a result the code is not pretty!
  84. X
  85. XPorting of the code to a MS_DOS environment took about 15 minutes (most of
  86. Xwhich was spent looking for the correct compiler flags and include files) and
  87. Xso can easily be used on considerably inferior systems without any problems.
  88. X
  89. X
  90. X - hd.c
  91. X
  92. XThis contains main, file open and close and the argument processor.
  93. X
  94. X - hexprint.c
  95. X
  96. XThe file hexprint.c contains a routine which can be used independantly
  97. Xfrom the main hd program in the debugging of your own programs for outputing
  98. Xareas of memory in a hexadecimal form.
  99. X
  100. XIt uses bcopy and bcmp or memcpy and memcmp routines depending on
  101. Xwhether the ATT compilation flag is set or not. Specifying ATT causes bcopy
  102. Xand bcmp to be used. Hence if compilation is being done on a machine running
  103. Xan AT&T version of unix use the ATT flag.
  104. X
  105. XThe H_OFFSETS and H_COMPRESS compilation flags switch on offset printing
  106. Xand compression, these need to be used in conjunction with the hex_compression
  107. Xand offset_print flags which must be set to true in the main program or
  108. Xenclosing scope of the called hexprint routine. The offset variable must be
  109. Xset each time the hexprint routine is entered otherwise the offset numbering
  110. Xwill always start at 0, so if you want to print out a number of buffers in
  111. Xsuccession with continuous offset numbering you need to update the
  112. Xoffset after each call of hexprint, with the size of the buffer
  113. Xlast passed to hexprint.
  114. X
  115. XOmission of H_OFFSETS and H_COMPRESS means you can forget everything I just
  116. Xsaid and just call the hexprint routine straight.
  117. X
  118. END_OF_FILE
  119. if test 2255 -ne `wc -c <'README'`; then
  120.     echo shar: \"'README'\" unpacked with wrong size!
  121. fi
  122. # end of 'README'
  123. fi
  124. if test -f 'hd.1' -a "${1}" != "-c" ; then 
  125.   echo shar: Will not clobber existing file \"'hd.1'\"
  126. else
  127. echo shar: Extracting \"'hd.1'\" \(2505 characters\)
  128. sed "s/^X//" >'hd.1' <<'END_OF_FILE'
  129. X.TH HD 1 "JUNE 2, 1989"
  130. X.UC 4
  131. X.SH NAME
  132. Xhd \- hexadecimal dump
  133. X.SH SYNOPSIS
  134. X.B hd
  135. X[
  136. X.B \-cnov
  137. X] [
  138. X.B \-b n
  139. X] [
  140. X.B \-sp
  141. X|
  142. X.B \-sn n
  143. X] [
  144. X.B \-sb n
  145. X] [
  146. X.B \-eb n
  147. X]
  148. X.B filename
  149. X.SH DESCRIPTION
  150. X.PP
  151. X.I hd
  152. Xproduces a hex dump of the specified file or the data that is piped into it
  153. Xfrom the standard input.
  154. X.PP
  155. XThe format of the dump is basically hex values on the left, in groups of 16
  156. Xand corresponding ascii values on the right. Only printable ascii values are
  157. Xprinted as themselves, all other ascii values are output as fullstops.
  158. X.PP
  159. XThere are various options to
  160. X.I hd
  161. Xwhich allow further formatting of the output, and control over the start and
  162. Xend of the output.
  163. X.PP
  164. XNote that byte counting starts from 0; record (block) counting starts from
  165. X1, after the start position which has been given (if any).
  166. X.PP
  167. XOptions:
  168. X.TP
  169. X.I \-b n
  170. X(block size):
  171. Xsets the block size to
  172. X.I n
  173. X(where
  174. X.I n
  175. Xmay be
  176. Xa decimal or C standard hexadecimal or octal number).
  177. XOmmision of this option will cause a continuous stream of output with no
  178. Xbreaks unless the \-n option is used.
  179. X.TP
  180. X.I \-c
  181. X(compress): compresses similar lines to a single char, "*". The complete line
  182. Xjust before the first char is the one which is repeated. This does not cross
  183. Xblock boundaries.
  184. X.TP
  185. X.I \-n
  186. X(numbering):
  187. Xenables block numbering, each block will be labelled giving its number from
  188. Xthe start of the file or the start block. The final block printed has its size
  189. Xprinted also, if it is less than the specified or default block size.
  190. XUsed without the \-b option causes 1024 byte blocks to be output.
  191. X.TP
  192. X.I \-o
  193. X(offset): prints the offset within the file on the very left hand
  194. Xside of the output.
  195. X.TP
  196. X.I \-sp n
  197. X(start posn, print):
  198. Xthis causes any blocking options that have been specified to start after the
  199. X.I n'th
  200. Xbyte in the file, the first
  201. X.I n
  202. Xbytes are printed in one continuous block. This option takes precedence over
  203. X.I \-sn.
  204. X.TP
  205. X.I \-sn n
  206. X(start posn, no print):
  207. Xoutput will commence starting after the
  208. X.I n'th
  209. Xbyte, the first
  210. X.I n
  211. Xbytes not being printed.
  212. X.I n
  213. Xmay be a decimal or C standard hexadecimal or octal number, and
  214. Xis held as a long to allow starting deep in a file.
  215. X.TP
  216. X.I \-v
  217. X(version):
  218. Xprint the version number of
  219. X.I hd.
  220. X.TP
  221. X.I \-sb n
  222. X(start block)
  223. Xoutput will commence starting after the
  224. X.I n'th
  225. Xblock in the file.
  226. X.TP
  227. X.I \-eb n
  228. X(end block)
  229. Xoutput will cease after the 
  230. X.I n'th
  231. Xblock in the file.
  232. X.SH "AUTHOR"
  233. XPeter Ruczynski
  234. X.br
  235. X.SH "FUTURE ENHANCEMENTS"
  236. X.PP
  237. XOffset printing in different bases, currently only in hexadecimal.
  238. X
  239. END_OF_FILE
  240. if test 2505 -ne `wc -c <'hd.1'`; then
  241.     echo shar: \"'hd.1'\" unpacked with wrong size!
  242. fi
  243. # end of 'hd.1'
  244. fi
  245. if test -f 'hd.c' -a "${1}" != "-c" ; then 
  246.   echo shar: Will not clobber existing file \"'hd.c'\"
  247. else
  248. echo shar: Extracting \"'hd.c'\" \(8162 characters\)
  249. sed "s/^X//" >'hd.c' <<'END_OF_FILE'
  250. X/******************************************************************************
  251. X*
  252. X*   Copyright P.J.Ruczynski 1990
  253. X*   This software is free for redistribution and re-use as long as this
  254. X*   copyright is included in all source files. This software is supplied
  255. X*   as is and no responsibilty is taken by the author for any problems
  256. X*   arising from this code.
  257. X*
  258. X* File name        -  hd.c
  259. X*
  260. X* Module name        -  HD
  261. X*
  262. X* Author        -  P.J.Ruczynski    <pjr@pyra.co.uk>
  263. X*
  264. X* First Release        -  16 Feb 1990
  265. X*
  266. X* Version number    -  1.4
  267. X*
  268. X* Description        -  A hexdump formatting tool, this can take input from
  269. X*               stdin or a file and print it to stdout in a neat
  270. X*               hexdump format.
  271. X*               Basically, a better/different(*) version of od.
  272. X*
  273. X*                (*) delete as you think appropriate :-)
  274. X*
  275. X*            Revision List
  276. X*
  277. X* pjr   08.05.89        Added offset numbering. 
  278. X*
  279. X* pjr   11.05.89        Added compressed output format. 
  280. X*
  281. X* pjr    30.05.89    Changed to allow any block size to be used, default
  282. X*            is still 1024 though.
  283. X*
  284. X* pjr    31.05.89    Added version printing option.
  285. X*
  286. X* pjr    02.06.89    Added start and end block specification.
  287. X*
  288. X******************************************************************************/
  289. X
  290. X#define VERSION        "1.4 (14.09.89)"
  291. X#define PRINTUSAGE    fprintf(stderr,"Usage: %s [-cnov] [-b n] [-sp|-sn n] [-sb n] [-eb n] [filename]\n", progname);
  292. X
  293. X/*
  294. X * This file contains the following routines:
  295. X *
  296. X * main()
  297. X * get_options()
  298. X * openfile()
  299. X * closefile()
  300. X */
  301. X
  302. X#include <stdio.h>
  303. X#include <sys/types.h>
  304. X#include <sys/file.h>
  305. X#include <string.h>
  306. X#include <fcntl.h>
  307. X#include "hexprint.h"
  308. X
  309. X#define    MAXLINE        256    /* Length of character strings. */
  310. X#define    MAX_BUFSIZ    1024    /* default read bufer size */
  311. X
  312. X/*
  313. X * these are control vars for the hexprint routine
  314. X */
  315. Xextern int offset_print;    /* only print offsets on request */
  316. Xextern long offset;        /* default start offset for printing */
  317. Xextern int hex_compression;    /* compress identical lines */
  318. X
  319. X/*
  320. X * option variables
  321. X */
  322. Xchar progname[MAXLINE];        /* Name of this program. */
  323. Xint b_start = 1;        /* start block no to be printed */
  324. Xint b_end = 0;            /* end block no to be printed */
  325. Xint b_size = MAX_BUFSIZ;    /* block size */
  326. Xint blocking_on = FALSE;    /* only block up on request */
  327. Xchar fname[MAXLINE];        /* name of input file (if any) */
  328. Xint block_numbering = FALSE;    /* output number of each block ? */
  329. Xint print_start_block = FALSE;    /* print the very first diff size block ? */
  330. Xint sb_size = 0;        /* staring block size */
  331. Xlong n_blocks = 0;        /* no blocks to print, 0 => all */
  332. X
  333. X/*
  334. X * get_options
  335. X *
  336. X * Read and process command line options.
  337. X */
  338. X/*****************************************************************************/
  339. Xvoid get_options(argc,argv)
  340. X/*****************************************************************************/
  341. Xint argc;
  342. Xchar *argv[];
  343. X{
  344. Xint i;
  345. X
  346. X    fname[0] = 0;    /* indicate no filename input */
  347. X    argc-- ; argv++ ;    /* skip the program name */
  348. X
  349. X    while (argc > 0)    /* now get the rest */
  350. X    {
  351. X      if (argv[0][0] == '-')
  352. X    switch (argv[0][1])
  353. X    {
  354. X    case 'b' :    /* block size to output */
  355. X        argv++;
  356. X        b_size = (int)strtol(&argv[0][0], (char **)NULL, 0);
  357. X        printf("block size = %d (0x%x) bytes\n",
  358. X                b_size,(unsigned)b_size);
  359. X        blocking_on = TRUE;
  360. X        argc--;
  361. X        break;
  362. X    case 'c' :
  363. X    case 'n' :
  364. X    case 'o' :
  365. X    case 'v' :
  366. X        i = 1;
  367. X        while (argv[0][i])
  368. X        {
  369. X            switch (argv[0][i])
  370. X            {
  371. X            case 'c' :    /* compress identical lines */
  372. X                hex_compression = TRUE;
  373. X                break;
  374. X            case 'n' :    /* switch on block numbering */
  375. X                block_numbering = TRUE;
  376. X                blocking_on = TRUE;
  377. X                break;
  378. X            case 'o' :    /* switch on offset printing */
  379. X                offset_print = TRUE;
  380. X                break;
  381. X            case 'v' :    /* print the version of this hd */
  382. X                printf("%s version %s Author P.Ruczynski\n",
  383. X                        progname,VERSION);
  384. X                break;
  385. X            default : PRINTUSAGE
  386. X                exit(1);
  387. X            } /* end of switch */
  388. X            i++;
  389. X        } /* end of while */
  390. X        break;
  391. X    case 'e' :    /* end block options */
  392. X        switch (argv[0][2])
  393. X        {
  394. X        case 'b' :    /* do not print starting block */
  395. X            argv++;
  396. X            b_end = strtol(&argv[0][0], (char **)NULL, 0);
  397. X            printf("last block printed = %d (0x%x) bytes\n",
  398. X                    b_end,(unsigned)b_end);
  399. X            argc--;
  400. X            break;
  401. X        default: PRINTUSAGE
  402. X            exit(1);
  403. X        }
  404. X        break;
  405. X    case 's' :    /* starting block options */
  406. X        switch (argv[0][2])
  407. X        {
  408. X        case 'p' :    /* print the starting block */
  409. X            print_start_block = TRUE;
  410. X            /* FALL THROUGH */
  411. X        case 'n' :    /* do not print starting block */
  412. X            argv++;
  413. X            sb_size = strtol(&argv[0][0], (char **)NULL, 0);
  414. X            printf("starting block size = %d (0x%x) bytes\n",
  415. X                    sb_size,(unsigned)sb_size);
  416. X            argc--;
  417. X            break;
  418. X        case 'b' :    /* get the starting block number */
  419. X            argv++;
  420. X            b_start = strtol(&argv[0][0], (char **)NULL, 0);
  421. X            printf("first block printed = %d (0x%x) bytes\n",
  422. X                    b_start,(unsigned)b_start);
  423. X            argc--;
  424. X            break;
  425. X        default: PRINTUSAGE
  426. X            exit(1);
  427. X        }
  428. X        break;
  429. X    case '?' :
  430. X    default  : PRINTUSAGE
  431. X        exit(1);
  432. X    } /* end of case */
  433. X    else    /* not an option so must be file name */
  434. X    {
  435. X        strcpy(&fname[0], &argv[0][0]);
  436. X        printf("input file = %s\n",fname);
  437. X    }
  438. X
  439. X    argc-- ;
  440. X    argv++ ;
  441. X
  442. X    } /* end of while loop */
  443. X
  444. X} /* end of get_options */
  445. X
  446. X/*
  447. X * openfile
  448. X *
  449. X * open the requested file or assign stdin for reading
  450. X */
  451. X/*****************************************************************************/
  452. Xint openfile(fname)
  453. X/*****************************************************************************/
  454. Xchar *fname;
  455. X{
  456. Xint fd;    /* file descriptor to read from */
  457. X
  458. X    if (fname[0] != 0)
  459. X        if ((fd = open(fname, O_RDONLY)) < 0)
  460. X        {
  461. X            perror(progname);
  462. X            exit(1);
  463. X        }
  464. X        else
  465. X            return(fd);
  466. X    else
  467. X        return(0);    /* if no file then stdin */
  468. X} /* end of openfile */
  469. X
  470. X
  471. X/*
  472. X * closefile
  473. X *
  474. X * close the given file descriptor
  475. X */
  476. X/*****************************************************************************/
  477. Xvoid closefile(fd)
  478. X/*****************************************************************************/
  479. Xint fd;
  480. X{
  481. X    if (fd != 0)
  482. X        close(fd);
  483. X} /* end of closefile */
  484. X
  485. X
  486. X/*
  487. X * printfile
  488. X *
  489. X * print the given file descriptor
  490. X */
  491. X/*****************************************************************************/
  492. Xvoid printfile(fd)
  493. X/*****************************************************************************/
  494. Xint fd;
  495. X{
  496. Xchar *buf;    /*[MAX_BUFSIZ]; */
  497. Xint n, x=0, y=0, i;
  498. Xlong bn = 1;
  499. X
  500. X    if ((buf = (char *)malloc(b_size)) == (char *)NULL)
  501. X    {
  502. X        printf("%s: error in block size memory allocation\n",progname);
  503. X        exit(1);
  504. X    }
  505. X
  506. X    /*
  507. X     * this first 'if' handles the start block case (if there is one)
  508. X     */
  509. X    if (sb_size != 0)
  510. X    {
  511. X        x = sb_size % MAX_BUFSIZ;    /* the very last bit */
  512. X        y = sb_size / MAX_BUFSIZ;    /* the no of full bufs */
  513. X        sb_size = MAX_BUFSIZ;        /* optimum read size. 1K */
  514. X
  515. X        if ((block_numbering) && (print_start_block))
  516. X            printf("start block\n");
  517. X
  518. X        for (i=0; i<=y; i++)
  519. X        {
  520. X            if (i==y)
  521. X                sb_size = x;
  522. X
  523. X            if (((n=read(fd, &buf[0], sb_size)) > 0) &&
  524. X                    (print_start_block))
  525. X                hexprint(&buf[0], n);
  526. X        
  527. X            offset += n;
  528. X        }
  529. X
  530. X        if (((blocking_on) || (hex_compression))
  531. X            && (print_start_block))
  532. X            printf("\n");
  533. X    }
  534. X        
  535. X    while ((n = read(fd, &buf[0], b_size)) > 0)
  536. X    {
  537. X        if ((block_numbering) && (bn >= b_start) &&
  538. X            ((bn <= b_end) || (b_end == 0)))
  539. X        {
  540. X            printf("block number %d (0x%x) ", bn, bn);
  541. X            if (n < b_size)
  542. X                printf("size = %d",n);
  543. X            printf("\n");
  544. X        }
  545. X
  546. X        if (blocking_on)
  547. X        {
  548. X            if (bn >= b_start)
  549. X            {
  550. X                if (b_end == 0)
  551. X                {
  552. X                    hexprint(&buf[0], n);
  553. X                    offset += n;
  554. X                }
  555. X                else if (bn <= b_end)
  556. X                {
  557. X                    hexprint(&buf[0], n);
  558. X                    offset += n;
  559. X                }
  560. X            }
  561. X            bn++;
  562. X        }
  563. X        else
  564. X        {
  565. X            hexprint(&buf[0], n);
  566. X            offset += n;
  567. X        }
  568. X
  569. X        if (((blocking_on) || (hex_compression)) &&
  570. X             (bn >= b_start) && ((bn <= b_end) || (b_end == 0)))
  571. X            printf("\n");
  572. X    }
  573. X
  574. X    free(buf);
  575. X
  576. X} /* end of printfile */
  577. X
  578. X/*
  579. X * main
  580. X */
  581. X/*****************************************************************************/
  582. Xmain(argc,argv)
  583. X/*****************************************************************************/
  584. Xint argc;
  585. Xchar **argv;
  586. X{
  587. Xint fd;    /* file descriptor to read from */
  588. X
  589. X    strcpy(progname, argv[0]);  /* Save this program name. */
  590. X    get_options(argc,argv);     /* Read and process command line options. */
  591. X
  592. X    fd = openfile(fname);        /* do this cos input maybe stdin */
  593. X    printfile(fd);
  594. X    closefile(fd);
  595. X
  596. X    exit(0);
  597. X
  598. X} /* end of main */
  599. X
  600. X
  601. END_OF_FILE
  602. if test 8162 -ne `wc -c <'hd.c'`; then
  603.     echo shar: \"'hd.c'\" unpacked with wrong size!
  604. fi
  605. # end of 'hd.c'
  606. fi
  607. if test -f 'hexprint.c' -a "${1}" != "-c" ; then 
  608.   echo shar: Will not clobber existing file \"'hexprint.c'\"
  609. else
  610. echo shar: Extracting \"'hexprint.c'\" \(4498 characters\)
  611. sed "s/^X//" >'hexprint.c' <<'END_OF_FILE'
  612. X/******************************************************************************
  613. X*
  614. X*   Copyright P.J.Ruczynski 1990
  615. X*   This software is free for redistribution and re-use as long as this
  616. X*   copyright is included in all source files. This software is supplied
  617. X*   as is and no responsibilty is taken by the author for any problems
  618. X*   arising from this code.
  619. X*
  620. X* File name        -  hexprint.c
  621. X*
  622. X* Module name        -  HEXPRINT
  623. X*
  624. X* Author        -  P.J.Ruczynski    <pjr@pyra.co.uk>
  625. X*
  626. X* First Release        -  16 Feb 1990
  627. X*
  628. X* Version number    -  1.4
  629. X*
  630. X* Description        -  A hexadecimal printing routine, will print the given
  631. X*               buffer with the hex on the left and the ascii on the
  632. X*               right.
  633. X*
  634. X*            Revision List
  635. X*
  636. X* pjr    08.05.89    Added offset numbering. This can be compiled out for
  637. X*            optimum performance.
  638. X*
  639. X* pjr    11.05.89    Added compressed output format. This can be compiled
  640. X*            out for optimum performance.
  641. X*
  642. X* pjr    02.08.89    Changed the bcopy and bcmp routines to use defines defined
  643. X*            in hexprint.h and controlled by compilation flag 'ATT'. With this
  644. X*            defined att routines are used, default means bsd routines are used.
  645. X*            Note that the compression stuff uses BCMP and BCOPY.
  646. X*
  647. X* pjr    04.08.89    Added output to file capability. This can be compiled in
  648. X*            by defining the H_FILEIO flag. Note that you also have to have
  649. X*            a file open with an fd of lfp, although this can be changed by
  650. X*            changing the decn below.
  651. X*
  652. X******************************************************************************/
  653. X#include <stdio.h>
  654. X#include "hexprint.h"
  655. X
  656. X#ifdef H_FILEIO
  657. Xextern FILE *lfp;
  658. X#else
  659. X#define lfp stdout
  660. X#endif /* H_FILEIO */
  661. X
  662. X#ifdef H_COMPRESS
  663. Xint hex_compression = FALSE;    /* compression is off as a default */
  664. X#endif /* H_COMPRESS */
  665. X
  666. X#ifdef H_OFFSETS
  667. Xint offset_print = FALSE;    /* only print offsets on request */
  668. Xlong offset = 0L;        /* default start offset for printing */
  669. X#endif /* H_OFFSET */
  670. X
  671. X/*
  672. X * hexprint
  673. X *
  674. X * routine to print a buffer out in hex form with ascii form on
  675. X * the right hand side
  676. X */
  677. X/*****************************************************************************/
  678. Xvoid hexprint(buf, buflen)
  679. X/*****************************************************************************/
  680. Xchar *buf;    /* ptr to data part of msg req, protocol format, to print     */
  681. Xint buflen;
  682. X{
  683. Xint i,j;
  684. Xchar string[H_SLEN];
  685. X
  686. X#ifdef H_COMPRESS
  687. Xint done_compression = FALSE;    /* have we done any compression ? */
  688. Xchar o_string[H_SLEN];            /* string prior to the current one */
  689. X#endif /* H_COMPRESS */
  690. X
  691. X#ifdef H_OFFSETS
  692. Xlong o;
  693. X    if (offset_print)
  694. X    {
  695. X        o = offset;
  696. X        fprintf(lfp,"%5x  ",o);
  697. X    }
  698. X#endif /* H_OFFSETS */
  699. X
  700. X    for (i=0; i<buflen; i++) {
  701. X        string[i%H_SLEN] = *buf++;
  702. X        if (i%H_SLEN == (H_SLEN-1)) {
  703. X#ifdef H_OFFSETS
  704. X            o += H_SLEN;
  705. X#endif /* H_OFFSETS */
  706. X#ifdef H_COMPRESS
  707. X            if (hex_compression)
  708. X                if (i == (H_SLEN-1))  /* first time around ? */
  709. X                    BCOPY(string, o_string, H_SLEN);
  710. X                else
  711. X                {
  712. X
  713. X                if (BCMP(o_string, string, H_SLEN) == 0)
  714. X                    {
  715. X                        fprintf(lfp,"* ");
  716. X                        done_compression = TRUE;
  717. X                        BCOPY(string, o_string, H_SLEN);
  718. X                        continue;
  719. X                    }
  720. X                    else if (done_compression)
  721. X                    {
  722. X                        fprintf(lfp,"\n");
  723. X#ifdef H_OFFSETS
  724. X                        if (offset_print)
  725. X                            fprintf(lfp, "%5x  ",o - H_SLEN);
  726. X#endif /* H_OFFSETS */
  727. X                        done_compression = FALSE;
  728. X                    }
  729. X                    BCOPY(string, o_string, H_SLEN);
  730. X                }
  731. X#endif /* H_COMPRESS */
  732. X            for (j=0; j<H_SLEN; j++)
  733. X                fprintf(lfp, "%2x ",(unsigned char)string[j]);
  734. X
  735. X            for (j=0; j<H_SLEN; j++)
  736. X                if (string[j] >= 0x20 && string[j] <= 0x7e)
  737. X                    fprintf(lfp, "%c",string[j]);
  738. X                else
  739. X                    fprintf(lfp, ".");
  740. X            fprintf(lfp, "\n");
  741. X#ifdef H_OFFSETS
  742. X            if ((i != (buflen -1)) && offset_print)
  743. X                fprintf(lfp, "%5x  ",o);
  744. X#endif /* H_OFFSETS */
  745. X        } else
  746. X            if ((i%H_SLEN < (H_SLEN-1)) && (i == buflen-1)) {
  747. X#ifdef H_COMPRESS
  748. X                if (done_compression)
  749. X                {
  750. X                    fprintf(lfp, "\n");
  751. X#ifdef H_OFFSETS
  752. X                    if (offset_print)
  753. X                        fprintf(lfp, "%5x  ",o);
  754. X#endif /* H_OFFSETS */
  755. X                    done_compression = FALSE;
  756. X                }
  757. X                BCOPY(string, o_string, H_SLEN);
  758. X#endif /* H_COMPRESS */
  759. X                for (j=0; j<=(i%H_SLEN); j++)
  760. X                    fprintf(lfp, "%2x ",(unsigned char)string[j]);
  761. X
  762. X                for (j=0; j<((H_SLEN-1)-(i%H_SLEN)); j++)
  763. X                    fprintf(lfp, "   ");
  764. X
  765. X                for (j=0; j<=(i%H_SLEN); j++)
  766. X                    if (string[j] >= 0x20 &&
  767. X                        string[j] <= 0x7e)
  768. X                        fprintf(lfp, "%c",string[j]);
  769. X                    else
  770. X                        fprintf(lfp, ".");
  771. X
  772. X                fprintf(lfp, "\n");
  773. X            }
  774. X    } /* end of for */
  775. X
  776. X#ifdef H_COMPRESS
  777. X    if (done_compression)
  778. X        fprintf(lfp, "\n");
  779. X#endif /* H_COMPRESS */
  780. X
  781. X} /* end of hexprint */
  782. X
  783. END_OF_FILE
  784. if test 4498 -ne `wc -c <'hexprint.c'`; then
  785.     echo shar: \"'hexprint.c'\" unpacked with wrong size!
  786. fi
  787. # end of 'hexprint.c'
  788. fi
  789. if test -f 'hexprint.h' -a "${1}" != "-c" ; then 
  790.   echo shar: Will not clobber existing file \"'hexprint.h'\"
  791. else
  792. echo shar: Extracting \"'hexprint.h'\" \(1174 characters\)
  793. sed "s/^X//" >'hexprint.h' <<'END_OF_FILE'
  794. X/******************************************************************************
  795. X*
  796. X*   Copyright P.J.Ruczynski 1990
  797. X*   This software is free for redistribution and re-use as long as this
  798. X*   copyright is included in all source files. This software is supplied
  799. X*   as is and no responsibilty is taken by the author for any problems
  800. X*   arising from this code.
  801. X*
  802. X* File name        -  hexprint.h
  803. X*
  804. X* Module name        -  HEXPRINT
  805. X*
  806. X* Author        -  P.J.Ruczynski    <pjr@pyra.co.uk>
  807. X*
  808. X* First Release        -  16 Feb 1990
  809. X*
  810. X* Version number    -  1.1
  811. X*
  812. X* Description        -  This file contains definitions required by the 
  813. X*               hexprint routine.
  814. X*
  815. X*            Revision List
  816. X*
  817. X* pjr    02.08.89    Changed the bcopy and bcmp routines to use defines defined
  818. X*            in hexprint.h and controlled by compilation flag 'ATT'. With this
  819. X*            defined att routines are used, default means bsd routines are used.
  820. X*            Note that the compression stuff uses BCMP and BCOPY.
  821. X*
  822. X******************************************************************************/
  823. X
  824. X#define    TRUE        1
  825. X#define    FALSE        0
  826. X#define    H_SLEN        16
  827. X
  828. X#ifdef ATT
  829. X#define BCMP memcmp
  830. X#define BCOPY(a,b,c) memcpy(b,a,c)
  831. X#else /* ucb */
  832. X#define BCMP bcmp
  833. X#define BCOPY bcopy
  834. X#endif
  835. END_OF_FILE
  836. if test 1174 -ne `wc -c <'hexprint.h'`; then
  837.     echo shar: \"'hexprint.h'\" unpacked with wrong size!
  838. fi
  839. # end of 'hexprint.h'
  840. fi
  841. echo shar: End of shell archive.
  842. exit 0
  843. -- 
  844.       -w---------    Pyramid Technology U.K.       Peter Ruczynski    
  845.     ---www-------    Pyramid House                 #include <std/disclaimer.h>
  846.   -----wwwww-----    Farnborough                   pjr@pyra.co.uk
  847. -------wwwwwww---    Hants GU14 7PL     England.   Wot no funny comment :-)
  848.  
  849.